import Vue from "vue";
import VueRouter from "vue-router";
import routes from "./router";
import $tool from "sept-ui/lib/utils/arms/tools.rich";

Vue.use(VueRouter);

// 解决重复定位同一路由报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err);
};

//是否开启nacos
let nacos = window.__GlobalConfig.extend.nacos;
let prefix = "";
if (nacos) {
    prefix = `/${nacos}/`;
}
if (nacos) {
    Loop(routes);
    function Loop(list) {
        list.forEach(item => {
            if (item.path != "/") {
                item.path = `${prefix}${item.path}`;
                item.path = item.path.replace(/\/\//, "/");
            }
            item.name = `${prefix}${item.name}`;
            if (item.children) {
                Loop(item.children);
            }
            if (item.redirect) {
                item.redirect = `${prefix}${item.redirect}`;
            }
        });
    }
}

const router = new VueRouter({
    routes,
    mode: "history",
    base: ""
});

//不重定向白名单
// let wpf = nacos ? `/${nacos}` : "";
// const whiteList = [`${wpf}/login`, `${wpf}/agent`];

// let sso = window.__GlobalConfig.extend.sso;

// router.beforeEach((to, from, next) => {
//     // 判断是否是白名单
//     if (whiteList.includes(to.path)) {
//         if(to.path.includes('login')&&sso){
//             next(`${prefix}overview`);
//         }else{
//             next();
//         }
//         return;
//     }
//     let permit = $tool.GetSessionStorage("CommData");
//     if (permit&&JSON.parse(permit).token) {
//         if (to.path == `${prefix}login`) {
//             next(from.fullPath);
//         } else {
//             if(nacos&&to.name&&to.name.indexOf(`/${nacos}/`)<0){
//                 next(`${prefix}${to.name}`);
//             }else{
//                 next();
//             }
//         }
//     } else {
//         //因为cookie-jwt在url访问时便会被网关拦截并鉴权，所以当代码运行到此处时若存在jwt则已经可以免登录
//         if(document.cookie.includes('jwt=')&&sso){
//             let cd = $tool.Decrypt(localStorage.getItem('__commData'));
//             if(cd){
//                 let item = JSON.parse(cd);
//                 item.org_rs_code = window.__GlobalConfig.rsCode;
//                 $tool.SetSessionStorage("CommData", JSON.stringify(item));
//                 next();
//                 return;
//             }
//         }
//         next(`${prefix}login`);
//     }
// });

export default router;
